package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.domain.Tran;
import com.bjpowernode.crm.workbench.service.CustomerService;
import com.bjpowernode.crm.workbench.service.TranService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resources;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @ClassName : TranController
 * @Author : 司猫子
 * @Date: 2021/1/13 13:56
 */
@Controller
public class TranController {
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private TranService tranService;
    @RequestMapping("/workbench/transaction/index.do")
    public String toTranIndex(Model model){
        List<DicValue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        List<DicValue> transactionTypeList = dicValueService.selectDicValueByTypeCode("transactionType");
        List<DicValue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        model.addAttribute("stageList",stageList);
        model.addAttribute("transactionTypeList",transactionTypeList);
        model.addAttribute("sourceList",sourceList);
        return "workbench/transaction/index";
    }

    @RequestMapping("/workbench/transaction/detailTran.do")
    public String toDetailTran(){

        return "workbench/transaction/detail";
    }

    @RequestMapping("/workbench/transaction/createTran.do")
    public Object createTran(Model model){
        List<User> userList = userService.selectAllUsers();
        List<DicValue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        List<DicValue> transactionTypeList = dicValueService.selectDicValueByTypeCode("transactionType");
        List<DicValue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        model.addAttribute("stageList",stageList);
        model.addAttribute("transactionTypeList",transactionTypeList);
        model.addAttribute("sourceList",sourceList);
        model.addAttribute("userList",userList);

        return "workbench/transaction/save";
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/queryCustomerByName.do")
    public Object queryCustomerByName(String customerName){
        List<Customer> customerList = customerService.selectCustomerByName(customerName);

        return customerList;
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/saveCreateTran.do")
    public Object saveCreateTran(Tran tran, HttpSession session){
        User user = (User) session.getAttribute(Contants.SUSSION_USER);
        tran.setId(UUIDUtils.getUUID());
        tran.setCreateTime(DateUtils.formatDateTime(new Date()));
        tran.setCreateBy(user.getId());
        ReturnObject returnObject = new ReturnObject();
        int i = tranService.insertTran(tran);
        if(i > 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("保存创建交易失败");
        }
        return returnObject;
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/getPossibilityByStageValue.do")
    public Object getPossibilityByStageValue(String stageValue){
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        String possibility = bundle.getString(stageValue);

        return possibility;
    }
}
